<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//科目管理
class Subject extends CommonController {

	function __construct(){
		
		parent::__construct();
		$this->load->model('User/SubjectModel','CurModel');
		
	}
	
	
	//start
	/**
	* @name 获取列表
	* @param page ture char 页面
	* @param size ture char 数量
	* @return 成功返回1
	*/
	//end
	function get_list(){
		$user_id = $this->user_id;
		
		
		$page		= $this->input->post('page');
		$size		= $this->input->post('size');		
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		
		$arr = array(
			'name'=>$this->input->post('name',null,''),
			'type'=>$this->input->post('type',null,''),
			'parent_id'=>$this->input->post('parent_id',null,-1),
			
		);
		
		$res = $this->CurModel->get_list($arr,$page,$size);
		$list = $res['list'];
		$leven_arr = array('一','二','三','四','五','六','七','八','九','十');
		foreach($list as $key=>$val){
			$leven = '';
			$leven = $this->get_leven($val['parent_id']);
			$list[$key]['leven'] = $leven_arr[$leven].'级';
		}
		
		
		$res['list'] = $list;
		
		$this->data_out($res);
	}
	
	function get_leven($pid){
		$levent = 0;
		if($pid==0){
			return $levent;
		}
		
		
		while(true){
			
			$info = $this->CurModel->getRow("id='{$pid}'");
			if(!$info){
				break;
			}
			if($info['parent_id']==$info['id']){
				break;
			}
			if($info['parent_id']==0){
				$levent++;
				break;
			}
			else{
				$levent++;
				$pid = $info['parent_id'];
			}
		}
		return $levent;
	}
	//start
	/**
	* @name 获取全部
	* @return 成功返回1
	*/
	//end
	function get_types(){
		//添加缓存开始，ling添加，20191012
		if(file_exists(APPPATH."cache/all_subject_cache.php")){
			include(APPPATH."cache/all_subject_cache.php");

			if($subject_cache){
				$this->data_out($subject_cache);
			}
		}

		$is_filter = $this->input->post('is_filter',null,0);
		
		$this->load->dmodel('subject_type','subject_type');
		$res = $this->subject_type->getAll();
		
		foreach($res as $key=>$val){
			$list = $this->get_childs($val['id'],0,$is_filter);
			if($list){
				foreach($list as $k=>$v){
					$list[$k] = $this->info_out($v);
				}
				
				$res[$key]['list'] = $list;
			}
		}
		
		//写入缓存
		parent::cache_write('subject_cache',$res);
		
		$this->data_out($res);
	}
	
	function get_childs($typdid,$parent_id=0,$is_filter=0){
		$arr = array(
			'parent_id'=>$parent_id,
			'type'=>$typdid
		);
		if(!$is_filter){
			$arr['is_enable'] = 1;
 		}
		
		
		$list = $this->CurModel->getAll($arr,"*",0,0);
		
		if(!$list){
			return false;
		}
		foreach($list as $key=>$val){
			//这里把科目代码放到科目名称前
			$list[$key]['name'] = $list[$key]['code'].$list[$key]['name'];
			$children = $this->get_childs($typdid,$val['id'],$is_filter);
			if($children){
				$list[$key]['children'] = $children;
			}
		}
		
		return $list;
	}
	function info_out($val){
		
		return $val;
	}

	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function get_info(){
		$user_id = $this->user_id;
		
		$result = parent::_get_info();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		$this->data_out($result);
	}
	
	//start
	/**
	* @name 获取信息
	* @param id ture char ID
	* @return 成功返回1
	*/
	//end
	function save(){
		
		if(!$this->check_priv('subject_update|subject_add')){
			$this->err_out(LANG_NOPRIV);
		}
		
		
		$id = $this->input->post('id',null,0);
		$is_more = $this->input->post('is_more',null,0);
		
		
		$arr = array(
			'name'=>$this->input->post('name',null,''),
			'code'=>$this->input->post('code',null,''),
			'type'=>$this->input->post('type',null,0),
			'parent_id'=>$this->input->post('parent_id',null,0),
			'is_enable'=>$this->input->post('is_enable',null,0),
		);
		
		$filter = array(
			'name'=>'请填写名称',
			'code'=>'请填写代码',
			'type'=>'请选择类别',
		);
		$result = $this->CheckData($filter,$arr);
		if($result!==false){
			$this->err_out($result);
		}
		
		if(!$is_more){
			if(empty($id)){
				$this->CurModel->add($arr);
			}
			else{
				$id = $this->CurModel->edit($arr,"id='{$id}'");
				
			}

			//删除缓存文件，ling添加，20191015
			if(file_exists(APPPATH."cache/all_subject_cache.php")){
				unlink(APPPATH."cache/all_subject_cache.php");
			}
		}
		else{
			
			$fg = explode("\n",$arr['name']);
			foreach($fg as $val){
				if(empty(trim($val))){
					continue;
				}
				$ls_arr = array(
					'name'=>trim($val),
					'type'=>$arr['type']
				);
				
				if(empty($id)){
					$this->CurModel->add($ls_arr);
				}
				else{
					$id = $this->CurModel->edit($ls_arr,"id='{$id}'");
				}
				
			}
			
		}
		
		$this->add_log("保存科目【{$id}】信息",'subject');
		$this->data_out(1);
	}
	
	
	
	//start
	/**
	* @name 删除
	* @param id ture int ID
	* @return 成功返回1
	*/
	//end
	function del(){
		
		if(!$this->check_priv('subject_del')){
			$this->err_out(LANG_NOPRIV);
		}
		
		$result = parent::_del();
		if(!$result){
			$this->err_out($this->getError());
		}
		
		//删除缓存文件，ling添加，20191015
		if(file_exists(APPPATH."cache/all_subject_cache.php")){
			unlink(APPPATH."cache/all_subject_cache.php");
		}
		
		$this->add_log("删除科目【{$result}】",'bumen');
		$this->data_out($result);
		
	}
	
	
	//start
	/**
	* @name 批量操作
	* @param ids ture int_array ID组合
	* @param act ture int 操作类型enable，disconnected，delete
	* @return 成功返回1
	*/
	//end
	function batch_act(){
		$ids = $this->input->post('ids',null,array());
		$act = $this->input->post('act',null,'');
		
		$act_arr = array('enable','disconnected','delete');
		if(!in_array($act,$act_arr)){
			$this->err_out('错误的错误');
		}
		if(!is_array($ids)){
			$ids = explode(',',$ids);
		}
		if(count($ids)<=0){
			$this->err_out('请选择成员');
		}
		$ls_ids = array();
		foreach($ids as $id){
			if(!empty($id)){
				$ls_ids[] = $id;
			}
		}
		
		if($act=='enable'){
			$this->CurModel->edit(array('is_enable'=>1),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='disconnected'){
			$this->CurModel->edit(array('is_enable'=>0),"id in(".implode(',',$ls_ids).")");
		}
		elseif($act=='delete'){
			$this->CurModel->del("id in(".implode(',',$ls_ids).")");
		}

		//删除缓存文件，ling添加，20191015
		if(file_exists(APPPATH."cache/all_subject_cache.php")){
			unlink(APPPATH."cache/all_subject_cache.php");
		}

		$this->data_out(1);
	}
	//start
	/**
	* @name 设置是否启用
	* @param id ture int ID
	* @param status ture int 状态
	* @return 成功返回1
	*/
	//end
	function set_enable(){
		if(!$this->check_priv('subject_update|subject_add')){
			$this->err_out(LANG_NOPRIV);
		}

		$id = $this->input->post('id',null,0);
		$status = $this->input->post('status',null,0);
		$info = $this->CurModel->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CurModel->edit(array('is_enable'=>$status),"id ='{$id}'");
		
		$this->data_out(1);
	}
	
	//没用
	function type_get_list(){
		$this->load->dmodel('subject_type','subject_type');
		
		$list = $this->subject_type->getAll();
		
		$this->data_out($list);
	}
	
	function type_save(){
		$name = $this->input->post('name',null,'');
		$id = $this->input->post('id',null,0);
		if(empty($name)){
			$this->err_out('名称不能为空 ');
		}
		
		$this->load->dmodel('subject_type','subject_type');
		
		if(empty($id)){
			$id = $this->subject_type->add(array('name'=>$name));
		}
		else{
			$this->subject_type->edit(array('name'=>$name),"id='{$id}'");
		}
		
		$this->data_out($id);
	}
	function type_del(){
		$id = $this->input->post('id',null,0);
		$this->load->dmodel('subject_type','subject_type');
		$info = $this->subject_type->_id_get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->subject_type->del("id='{$id}'");
		$this->data_out($id);
	}
	
	function import_data(){
		$mark = $this->input->post('mark',null,'');
		$this->load->dmodel('upload_data','upload_data');
		
		$info = $this->upload_data->_field_get_info($mark,'token');
		if(!$info){
			$this->err_out('文件不存在');
		}
		
		$file_path = FCPATH .'upload/'.$info['path'];
		if(!file_exists($file_path)){
			$this->err_out('文件路径不存在');
		}
		
		$str = file_get_contents($file_path);
		$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
		if($encode!='UTF-8'){
			$str_encode = mb_convert_encoding($str, 'UTF-8', $encode);
			if($str_encode){
				$str = $str_encode;
			}
		}
		$files = explode("\n",$str);
		
		$this->load->dmodel('subject_type','subject_type');
		
		$typelist = $this->subject_type->getAll();
		$arr = array();
		$err = array();
		foreach($files as $key=>$val){
			if($key==0){
				continue;
			}
			if(empty($val)){
				continue;
			}
			
			$fg = explode(',',$val);
			
			if(count($fg)<2){
				$err[] = '行数据错误';
				continue;
			}
			$name = trim($fg[0]);
			$type_name = trim($fg[1]);
			if(empty($name) || empty($type_name)){
				$err[] = '名称或类型不能为空';
				continue;
			}
			$typeid = 0;
			
			foreach($typelist as $v){
				if($v['name']==$type_name){
					$typeid = $v['id'];
					break;
				}
			}
			
			if(empty($typeid)){
				$err[] = '找不到存在的类型';
				continue;
			}

			
			$ls_arr = array(
				'name'=>$name,
				'type'=>$typeid
			);
			$info = $this->CurModel->_where_exist($ls_arr);
			
			if($info){
				$err[] = '数据已存在';
				continue;
			}
			$arr[] = $ls_arr;
		}
		
		if(count($arr)<=0){
			if(count($err)>0){
				$this->err_out(implode("\n",$err));
			}
			else{
				$this->err_out("数据筛选后为空");
			}
		}
		
		foreach($arr as $val){
			$this->CurModel->add($val);
		}
		
		$this->data_out(1);
	}
	
	function export_data(){
		$arr = array(
			'name'=>$this->input->post('name',null,''),
			'type'=>$this->input->post('type',null,''),
		);
		
		$list = $this->CurModel->get_list($arr);
		
		
		$data = "名称,类型\n";
		foreach($list as $val){
			$ls_arr = array($val['name'],$val['type_name']);
			$data .= implode(',',$ls_arr)."\n";
		}
		
		$dir =  'upload/res/tmp/';
		$file_name = 'subject_'.time().'_'.rand(10000,99999).'.csv';
		
		if(!file_put_contents(FCPATH . $dir . $file_name,$data)){
			$this->err_out("生成数据错误");
		}
		$url = base_url($dir.$file_name);
		$this->data_out($url);
	}
}
